package net.zjitc.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.zjitc.entity.*;
import net.zjitc.mapper.*;
import net.zjitc.service.IYonghuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther CodeGenerator-gmt
 * @create 2022-04-25 11:00:47
 * @describe 服务实现类
 */
@Service
@Transactional
public class YonghuServiceImpl extends ServiceImpl<YonghuMapper, Yonghu> implements IYonghuService {
    @Autowired
    private YonghuMapper yonghuMapper;
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private MycollectMapper mycollectMapper;
    @Autowired
    private MycompanyMapper mycompanyMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<Yonghu> getYonghus(String querystring, Integer pagenum, Integer pagesize) {
        Page<Yonghu> page=new Page<>(pagenum,pagesize);
        if (querystring.length()==0){
            page = yonghuMapper.selectPage(page,null);
        }else {
            QueryWrapper<Yonghu> wrapper=new QueryWrapper<>();
            wrapper.like("username",querystring).or().like("name",querystring);
            page = yonghuMapper.selectPage(page, wrapper);
        }
        return page;
    }

    @Override
    public int deleteById(int id) {

        Yonghu yonghu = yonghuMapper.selectById(id);
        //删除用户的收藏
        QueryWrapper<Mycollect> wrapper1=new QueryWrapper<>();
        wrapper1.eq("username",yonghu.getUsername());
        mycollectMapper.delete(wrapper1);
        //删除用户关注
        QueryWrapper<Mycompany> wrapper2=new QueryWrapper<>();
        wrapper2.eq("username",yonghu.getUsername());
        mycompanyMapper.delete(wrapper2);
        //删除用户投递的
        QueryWrapper<Apply> wrapper3=new QueryWrapper<>();
        wrapper3.eq("yonghuid",id);
        applyMapper.delete(wrapper3);
        //删除用户
        QueryWrapper<User> wrapper4=new QueryWrapper<>();
        wrapper4.eq("username",yonghu.getUsername());
        userMapper.delete(wrapper4);

        //删除用户
        int i = yonghuMapper.deleteById(id);


        return i;
    }

    @Override
    public Yonghu selectByUsername(String username) {
        QueryWrapper<Yonghu> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        Yonghu yonghu = yonghuMapper.selectOne(wrapper);
        return yonghu;
    }
}
